var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
		when('/', {templateUrl: 'districtmast.html', controller: districtcont}).
		when('/cancel/', {templateUrl: 'districtmast.html', controller: districtcont}). 
		otherwise({redirectTo: '/'});
}]);

function districtcont($scope, $http,$window,$location,$filter)
 {
    $scope.count = 0;    
	$scope.Math = window.Math;	
	var d = new Date();	
	$scope.ed = false;	
	$scope.typearr = [];
		
	$http.get('districtmastdata.php').success(function(data) {
		$scope.districtmastdata = data;
			alert("Please enter all the fields" );
		for(var i=0;i<data.length;i++)
			$scope.typearr[i] = data[i].name;
    });		
	
	$scope.save= function()
	{
		if(!$scope.district.name )
			alert("Please enter all the fields");
		else{
			var valid=true;			
			for(var i=0;i<$scope.typearr.length;i++)
				if($scope.district.name == $scope.typearr[i])
					valid=false;												
			if(!valid)
				alert("This District Name already exists");
			else{
				var sql = 'districtmast_cud.php?q=c&dataset='+JSON.stringify($scope.district);
				$http.get(sql).success(function(data) {									
					alert("Record saved");
				});
				$scope.cancel();
			}
		}			
	}
	
	$scope.edit= function(id)
	{
		for(i in $scope.districtmastdata) {
			if($scope.districtmastdata[i].id == id)
           {          
              $scope.district = angular.copy($scope.districtmastdata[i]);				
           }
        }
		$scope.ed = true;
	}
	
	$scope.upd= function()
	{
		if(!$scope.district.name)
			alert("Please enter all the fields");
		else{
			var sql = 'districtmast_cud.php?q=u&dataset='+JSON.stringify($scope.district);
			$http.get(sql).success(function(data) {							
				alert("Record "+$scope.district.id+" updated");
			});
			$scope.cancel();		
		}		
	}
	
	$scope.dlt= function(id)
	{		
		var sql = 'districtmast_cud.php?q=d&id='+id;		
		$http.get(sql).success(function(data) {				
			alert("Record "+id+" deleted");
		});
		$scope.cancel();
	}
	
	$scope.cancel= function()
	{		 
		$scope.activePath = null;
		$scope.activePath = $location.path('districtmast.php');	
	};
 }
 
 app.directive('autoComplete', function($timeout) {
    return function(scope, iElement, iAttrs) {
            iElement.autocomplete({
                source: scope[iAttrs.uiItems],
                select: function() {
                    $timeout(function() {
                      iElement.trigger('input');
                    }, 0);
                }
            });
    };
});

app.directive('capitalize', function() {
   return {
     require: 'ngModel',
     link: function(scope, element, attrs, modelCtrl) {
        var capitalize = function(inputValue) {
           if(inputValue == undefined) inputValue = '';
           var capitalized = inputValue.toUpperCase();
           if(capitalized !== inputValue) {
              modelCtrl.$setViewValue(capitalized);
              modelCtrl.$render();
            }         
            return capitalized;
         }
         modelCtrl.$parsers.push(capitalize);
         capitalize(scope[attrs.ngModel]);  // capitalize initial value
     }
   };
});
